package com.hbu.product.hbumallproduct.controller;

import com.hbu.common.utils.R;
import com.hbu.product.hbumallproduct.entity.AttrGroupEntity;
import com.hbu.product.hbumallproduct.service.AttrAttrgroupRelationService;
import com.hbu.product.hbumallproduct.service.AttrGroupService;
import com.hbu.product.hbumallproduct.service.AttrService;
import com.hbu.product.hbumallproduct.service.CategoryService;
import com.hbu.product.hbumallproduct.vo.AddAttrRelationVO;
import com.hbu.product.hbumallproduct.vo.AttrRelationDeleteVO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * ???Է??
 *
 * @author zhaoxiaohan
 * @email 13315217827@163.com
 * @date 2022-10-27 22:52:07
 */
@RestController
@RequestMapping("hbumallproduct/attrgroup")
@AllArgsConstructor
public class AttrGroupController {

    private AttrGroupService attrGroupService;

    private CategoryService categoryService;

    private AttrService attrService;

    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping(value = "/list/{categoryId}", method = RequestMethod.GET)
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long categoryId) {
        return R.ok().put("page", attrGroupService.queryPage(params, categoryId));
    }

    /**
     * 获取关联关系
     *
     * @return
     */
    @RequestMapping(value = "/{attrgroupId}/attr/relation", method = RequestMethod.GET)
    public R getAttrRelation(@PathVariable Long attrgroupId) {
        return R.ok().put("data", attrService.getAttrRelation(attrgroupId));
    }

    @RequestMapping(value = "/attr/relation/delete", method = RequestMethod.POST)
    public R relationDelete(@RequestBody AttrRelationDeleteVO[] attrRelationDeleteVOList) {
        attrAttrgroupRelationService.relationDelete(attrRelationDeleteVOList);
        return R.ok();
    }

    /**
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     *
     * @return
     */
    @RequestMapping(value = "{attrgroupId}/noattr/relation", method = RequestMethod.GET)
    public R noAttrRelation(@RequestParam Map<String, Object> params, @PathVariable Long attrgroupId) {
        return R.ok().put("page", attrService.getNoAttrRelation(params, attrgroupId));
    }

    /**
     * 获取分类下所有分组&关联属性
     */
    @RequestMapping(value = "/{catelogId}/withattr" ,method = RequestMethod.GET)
    public R getAttrByCatelogId(@PathVariable Long catelogId){
        return R.ok().put("data", attrAttrgroupRelationService.getAttrByCatelogId(catelogId));
    }

    @RequestMapping(value = "/attr/relation", method = RequestMethod.POST)
    public R addAttrRelation(@RequestBody List<AddAttrRelationVO> addAttrRelationVOList) {
        attrAttrgroupRelationService.addAttrRelation(addAttrRelationVOList);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] path = categoryService.findCategoryPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
